package creator

import (
	"gitee.com/kristas/booting-go/framework/common/util/lang"
)

// ComponentFile base component creator
type ComponentFile struct {
	ComponentName string
	Data          map[string]interface{}
	Type          string
}

func (c *ComponentFile) Create() error {
	g := NewGoFile(c.Type, c.Type, c.ComponentName, true)
	if c.Data != nil {
		for k, v := range c.Data {
			g.SetAttribute(k, v)
		}
	}
	g.SetAttribute("ComponentName", lang.NewString(c.ComponentName).CamelCase())
	return g.Create()
}

// FilterFile filter creator
type FilterFile struct {
	FilterName string
	Pattern    string
	Order      int
}

func (f *FilterFile) Create() error {
	filter := &ComponentFile{
		ComponentName: f.FilterName,
		Data: map[string]interface{}{
			"Pattern": f.Pattern,
			"Order":   f.Order,
		},
		Type: "filter",
	}
	return filter.Create()
}

// ControllerFile controller creator
type ControllerFile struct {
	ControllerName string
	Group          string
}

func (c *ControllerFile) Create() error {
	controller := &ComponentFile{
		ComponentName: c.ControllerName,
		Data: map[string]interface{}{
			"Group": c.Group,
		},
		Type: "api",
	}
	return controller.Create()
}

// RepositoryFile repository creator
type RepositoryFile struct {
	RepoName   string
	Table      string
	DataSource string
}

func (s *RepositoryFile) Create() error {
	g := &ComponentFile{
		ComponentName: s.RepoName,
		Type:          "repository",
		Data: map[string]interface{}{
			"DataSource": s.DataSource,
			"Table":      s.Table,
		},
	}
	return g.Create()
}

// ServiceFile service creator
type ServiceFile struct {
	ServiceName string
}

func (s *ServiceFile) Create() error {
	g := &ComponentFile{
		ComponentName: s.ServiceName,
		Type:          "service",
	}
	return g.Create()
}
